import TcVod from "vod-js-sdk-v6";
import { tipText } from "./store";
export const beforeUploadVideo = (file: any) => {
  return new Promise((resolve) => {
    let video = createElNode("video");
    video.preload = "metadata";
    video.onloadedmetadata = function () {
      let url = video.src;
      if (url.startsWith("blob:")) url = url.substr(5);
      window.URL.revokeObjectURL(url);
      let duration = video.duration;
      resolve(parseInt(duration));
    };
    video.src = URL.createObjectURL(file);
  });
};
export const uploadVideo = (
  mediaFile: any,
  getSignature: any,
  callback: any
) => {
  const tcVod = new TcVod({ getSignature: async () => getSignature });
  const uploader = tcVod.upload({ mediaFile });
  uploader.on("media_progress", (info: any) => {
    tipText.value = (info.percent * 100).toFixed(2) + "%";
  });
  uploader
    .done()
    .then((doneResult: any) => {
      callback && callback(doneResult);
      tipText.value = $t("placeholder71");
    })
    .catch((err: any) => {
      console.log(err);
    });
};
